View Javadoc

1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Field.java
6    *
7    * Created on 15-Dec-2004 at 1:49:09 PM
8    */
9   package ca.uhn.cache.internal.hibernate.impl;
10  
11  import java.util.Map;
12  
13  import org.apache.commons.lang.builder.EqualsBuilder;
14  import org.apache.commons.lang.builder.HashCodeBuilder;
15  import org.apache.commons.lang.builder.ToStringBuilder;
16  
17  import ca.uhn.cache.IQueryParam;
18  
19  
20  
21  /***
22   * Base class for all kinds of persistent fields.
23   * 
24   * Subclasses must provide made the <code>Field</code>'s constructor public
25   * and provide default constructor as well.
26   * 
27   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
28   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:40 $ by $Author: bryan_tripp $
29   * 
30   * @hibernate.class table="field"
31   */
32  public abstract class Field extends HibernateObject implements Comparable {
33      
34      private Record myRecord;
35      private String myDimensionName;
36       
37      /***
38       * @param theRecord The record this field belongs to.
39       * @param theDimensionName The name of the dimension of this field.
40       */
41      protected Field( Record theRecord, String theDimensionName ) {
42          myRecord = theRecord;
43          myDimensionName = theDimensionName;
44          
45          if (myRecord != null) {
46              myRecord.addField(this);
47          }
48      }
49      
50      /***
51       * TODO refactor the parameter theDimensionNameToDimensionMap to use a typed map!
52       * 
53       * @param theDimensionNameToDimensionMap Map of dimension name to dimension.
54       * @return The <code>IQueryParam</code> implementation corresponding to this field.
55       */
56      public abstract IQueryParam toQueryParam( Map theDimensionNameToDimensionMap );
57  
58      /***
59       * @return Returns the dimensionName.
60       * 
61       * @hibernate.property not-null="true"
62       */
63      public String getDimensionName() {
64          return myDimensionName;
65      }
66      /***
67       * @param theDimensionName The dimensionName to set.
68       */
69      public void setDimensionName( String theDimensionName ) {
70          myDimensionName = theDimensionName;
71      }
72      /***
73       * @return Returns the record.
74       * 
75       * @hibernate.many-to-one not-null="true" class="ca.uhn.cache.internal.hibernate.impl.Record"
76       */
77      public Record getRecord() {
78          return myRecord;
79      }
80      /***
81       * @param theRecord The record to set.
82       */
83      public void setRecord( Record theRecord ) {
84          myRecord = theRecord;
85      }
86      
87      
88      /***
89       * {@inheritDoc}
90       */
91      public int hashCode() {
92          return new HashCodeBuilder()
93                      .append( getHibernateId() )
94                      .append( getRecord().getHibernateId() )
95                      .append( getDimensionName() ).toHashCode();
96      }
97      
98      
99      /***
100      * {@inheritDoc}
101      */
102     public boolean equals( Object theObj ) {
103         boolean retVal = false;
104         if (theObj instanceof Field) {
105             Field otherField = (Field) theObj;
106             retVal = new EqualsBuilder()
107                 .append( getHibernateId(), otherField.getHibernateId() )
108                 .append( getRecord().getHibernateId(), otherField.getRecord().getHibernateId() )
109                 .append( getDimensionName(), otherField.getDimensionName() ).isEquals();
110         }
111         return retVal;
112     }
113     
114     
115     /***
116      * {@inheritDoc}
117      */
118     public String toString() {
119         return new ToStringBuilder( this )
120                     .append( getHibernateId() )
121                     .append( getRecord().getHibernateId() )
122                     .append( getDimensionName() ).toString();
123     }
124     
125     
126     /***
127      * {@inheritDoc}
128      */
129     public int compareTo( Object theObj ) {
130         if (!(theObj instanceof Field)) {
131             throw new ClassCastException( "Can't compadre with a NON instance of Field" );
132         }
133         Field otherField = (Field) theObj;
134         return getDimensionName().compareTo( otherField.getDimensionName() ) ;
135     }
136     
137 }